package com.aube.plugin.rank.model;

import com.huyn.baseframework.model.Character;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by huyaonan on 16/8/9.
 */
public class CharacterGenerator {

    public static CharacterProxy generatorItem(Object obj) {
        CharacterProxy cItem = new CharacterProxy();
        LinkedHashMap<String, Object> lhm = (LinkedHashMap<String, Object>) obj;
        Iterator<String> keyIterator = lhm.keySet().iterator();
        while (keyIterator.hasNext()) {
            String key = keyIterator.next();
            Object value = lhm.get(key);

            if(Character.ACTOR_NAME.equalsIgnoreCase(key)) {
                cItem.actorName = (String) value;
            } else if(Character.ACTOR_PIC.equalsIgnoreCase(key)) {
                cItem.actorPic = (String) value;
            } else if("actorDesc".equalsIgnoreCase(key)) {
                cItem.actorDesc = (String) value;
            } else if("guestName".equalsIgnoreCase(key)) {
                cItem.actorName = (String) value;
            } else if("guestPic".equalsIgnoreCase(key)) {
                cItem.actorPic = (String) value;
            } else if(Character.PRAISE_COUNT.equalsIgnoreCase(key)) {
                cItem.praiseCount = (int) ((double) value);
            } else if("weekPraiseCount".equalsIgnoreCase(key)) {
                cItem.weekPraiseCount = (int) ((double) value);
            } else if(Character.RELATED_ID.equalsIgnoreCase(key)) {
                cItem.relatedId = (String) value;
            } else if("guestDesc".equalsIgnoreCase(key)) {
                cItem.actorDesc = (String) value;
            } else if("praised".equalsIgnoreCase(key)) {
                cItem.praised = (String) value;
            } else if("period".equalsIgnoreCase(key)) {
                cItem.period = (String) value;
            } else if("isNotClick".equalsIgnoreCase(key)) {
                cItem.isNotClick = (String) value;
            } else if("weekDesc".equalsIgnoreCase(key)) {
                cItem.weekDesc = (String) value;
            } else if("colorRank".equalsIgnoreCase(key)) {
                cItem.colorRank = (int) ((double) value);
            } else if("untilLastWeekCount".equalsIgnoreCase(key)) {
                cItem.untilLastWeekCount = (int) ((double) value);
            }
        }
        return cItem;
    }

    public static RankModel generatorRankModel(Object obj) {
        RankModel rankModel = new RankModel();

        LinkedHashMap<String, Object> lhm = (LinkedHashMap<String, Object>) obj;
        Iterator<String> keyIterator = lhm.keySet().iterator();

        while (keyIterator.hasNext()) {
            String key = keyIterator.next();
            Object value = lhm.get(key);

            if("type".equalsIgnoreCase(key)) {
                rankModel.type = (String) value;
            } else if("desc".equalsIgnoreCase(key)) {
                rankModel.desc = (String) value;
            } else if("pic".equalsIgnoreCase(key)) {
                rankModel.pic = (String) value;
            } else if("week".equalsIgnoreCase(key)) {
                rankModel.week = (String) value;
            } else if("rank".equalsIgnoreCase(key)) {
                List<CharacterProxy> characters = new ArrayList<>();
                CharacterProxy cItem;
                List list = (List) value;
                for(Object item : list) {
                    cItem = CharacterGenerator.generatorItem(item);
                    characters.add(cItem);
                }

                rankModel.characters = characters;
            }
        }

        return rankModel.sort();
    }

}
